package com.qst.action.schedule;

import com.qst.BaseServlet;
import com.qst.ExamException;
import com.qst.RequestUtil;
import com.qst.WebUtil;
import com.qst.entity.Schedule;
import com.qst.entity.User;
import com.qst.service.ScheduleService;
import com.qst.service.impl.ServiceFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


//@WebServlet("/schedule/save.action")
public class SaveServlet extends BaseServlet {
    private ScheduleService scheduleService = ServiceFactory.getService(ScheduleService.class);

    @Override
    protected void doAction(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Schedule schedule = new Schedule();
        try {
            User user = getCurrentUser(req);
            schedule.setBeginDate(RequestUtil.getString(req, "beginDate"));
            schedule.setEndDate(RequestUtil.getString(req, "endDate"));
            schedule.setDuration(RequestUtil.getInt(req, "duration"));
            schedule.setCreateDate(RequestUtil.getDate(req, "createDate"));
            schedule.setStatus(1);
            schedule.setAssessmentId(RequestUtil.getInt(req, "assessmentId"));
            schedule.setTeamId(RequestUtil.getInt(req, "teamId"));
            schedule.setQuestionNumber(RequestUtil.getInt(req, "questionNumber"));
            schedule.setCreatorId(user.getId());
            scheduleService.saveSchedule(schedule);
            WebUtil.redirect(req, resp, "/schedule/view.action?id="+schedule.getId());
        } catch (ExamException ex) {
            ex.printStackTrace();
            addError(req, ex.getMessage());
            req.setAttribute("schedule", schedule);
            WebUtil.forward(req, resp, "/schedule/create.action");
        }
    }
}
